<template>
  <div>
    <common-head title="商品列表"></common-head>
    <div class="item-top">
      <div :class="['sale', {
        active: orderBy.type === 'sale'
      }]"
        @click="bySale"
      >
        <span>销量</span>
        <div :class="['angle','top',{
          active: orderBy.type === 'sale' && orderBy.sort === 1
        }]"></div>
        <div :class="['angle','bottom',{
          active: orderBy.type === 'sale' && orderBy.sort === 0
        }]"></div>
      </div>
      <div :class="['price', {
        active:  orderBy.type === 'price'
      }]"
        @click="byPrice"
      >
        <span>价格</span>
        <div :class="['angle','top',{
          active: orderBy.type === 'price' && orderBy.sort === 1
        }]"></div>
        <div :class="['angle','bottom',{
          active: orderBy.type === 'price' && orderBy.sort === 0
        }]"></div>
      </div>
    </div>
    <div class="wrap">
      <items @click="enterDetail" :items="items"></items>
    </div>
  </div>
</template>

<script>
import { fetchItems } from 'api'
import CommonHead from 'components/CommonHead'
import Items from './components/Items'
export default {
  data () {
    return {
      orderBy: {
        type: 'sale', // 按照销量还是价格排序  sale  price
        sort: 0     // 升序还是降序  1 降序  0升序
      },
      order_by: 'ordersDown', // 传递参数 默认销量降序
      items: []
    }
  },
  methods: {
    enterDetail ({id}) {
      this.$router.push(`/detail/${id}`)
    },
    bySale () {
      // 点击按照 销量排序
      // 是不是从价格点击过来
      if(this.orderBy.type === 'price'){
        // 按照 销量降序
        this.orderBy.type = 'sale'
        this.orderBy.sort = 1
        this.order_by = 'ordersDown'
      }

      // 点击切换销量升序还是降序
      if(this.orderBy.sort){
        this.orderBy.sort = 0
        this.order_by = 'ordersUp'

      }else{
        this.orderBy.sort = 1
        this.order_by = 'ordersDown'
      }

      this.fetchItems()
    },
    byPrice () {
      // 点击按照 价格排序
      // 是不是从销量点击过来
      if(this.orderBy.type === 'sale'){
        // 按照 销量降序
        this.orderBy.type = 'price'
        this.orderBy.sort = 0
        this.order_by = 'priceUp'
      }

      // 点击切换价格升序还是降序
      if(this.orderBy.sort){
        this.orderBy.sort = 0
        this.order_by = 'priceDown'
      }else{
        this.orderBy.sort = 1
         this.order_by = 'priceUp'
      }

      this.fetchItems()
    },
    fetchItems () {
      const params = {
        categoryId: this.$route.query.cateId,
        orderBy:this.order_by
      }
      fetchItems(params).then(res=>{
        if (res.data.code === 0) {
          this.items = res.data.data
        }
      })
    }
  },
  created () {
    this.fetchItems()
  },
  components: {
    CommonHead,
    Items
  }
}
</script>

<style lang="scss" scoped>
.wrap{
  padding-top:96px;
}
.item-top{
  height:50px;
  position: fixed;
  top:46px;
  left:0;
  width:100%;
  background:#f5f5f5;
  display: flex;
  align-items: center;
  justify-content: space-around;
  font-size: 14PX;
  color:#333;
  .sale{
    &.active{
      color:#f66;
    }
    position: relative;
    .angle{
      width:0;
      height:0;
      border: 6px solid transparent;
      position: absolute;
      right: -15px;

    }
    .top{
      border-bottom-color:#333;
      &.active{
        border-bottom-color:#f66;
      }
      top:-7px;
    }
    .bottom{

      border-top-color:#333;
      &.active{
        border-top-color:#f66;
      }
      top:9px;
    }
  }
  .price{
    &.active{
      color:#f66;
    }
    position: relative;
    .angle{
      width:0;
      height:0;
      border: 6px solid transparent;
      position: absolute;
      right: -15px;

    }
    .top{
      border-bottom-color:#333;
      &.active{
        border-bottom-color:#f66;
      }
      top:-7px;
    }
    .bottom{
      &.active{
        border-top-color:#f66;
      }
      border-top-color:#333;
      top:9px;
    }
  }

}
</style>
